<?
    /***************************************************************************************************
     PROGRAM DESCRIPTION :  PROGRAM TO VIEW reverse sles of the affiliate
      VARIABLES          :   $MERCHANTID           =merchantid
                             $transid              =transactionid
                             $pgmid                =programid
                             $mode                 =get input type
                             $tranStat             =getting Transaction Statistics from transaction.php
                             $details              =getting Affiliate Staistics   from transaction.php
  //*************************************************************************************************/


  include "transactions.php";
  include "../mail.php";

  /*********************variables**********************************************/
  $MERCHANTID                        =$_SESSION['MERCHANTID'];         //merchantid
  $transid                           =trim($_POST['transid']);         //transactionid
  $transid                           =explode('~',$transid);
  $pgmid                             =trim($_GET['pgmid']);            //programid
  $mode                              =trim($_POST['mode']);            //get input type
  /****************************************************************************/

  /****************************reversale***************************************/
  switch($mode)
  {
      case 'Reverse Sale':    //Reverse Sale
              $sql                ="update partners_transaction  set transaction_status='reverserequest' where transaction_id=$transid[0]  ";
              mysql_query($sql);
              /*$sql="select * from partners_login,partners_joinpgm where joinpgm_id=$transid[2] and login_flag='a' and joinpgm_affiliateid=login_id";
                  $ret1=mysql_query($sql);
                  $row=mysql_fetch_object($ret1);
                  $to=$row->login_email;*/
           //   MailEvent("Reverse Sale",$MERCHANTID,$transid[2],$to,$transid[0]);
              $pgmid        =0;
              break;

   }
   /**************************************************************************/


   /*****************select pgm***********************************************/
  switch($pgmid) //select pgm
  {
      case '0':     //all
              $sql                        ="select * from partners_joinpgm,partners_transaction,partners_program where joinpgm_merchantid=$MERCHANTID and transaction_type='sale' and transaction_status  not like ('pending') and transaction_status  not like ('reversed') and joinpgm_id=transaction_joinpgmid and program_id=joinpgm_programid AND transaction_recur not like '1'  ";
              break;
      default :    //selected pgms
              $sql                        ="select * from partners_joinpgm,partners_transaction,partners_program where program_id=$pgmid and transaction_type='sale' and transaction_status not  like ('pending')  and transaction_status  not like ('reversed') and joinpgm_id=transaction_joinpgmid and program_id=joinpgm_programid AND transaction_recur not like '1' ";
              break;
   }  
  $result                                        =mysql_query($sql);
  $result1                                       =mysql_query($sql);
  /****************************************************************************/



  /**********************getting statistics************************************/
  if (mysql_num_rows($result)>0)  //checking for records
   {
              $rows                        =mysql_fetch_object($result1);  //for first time
              $id                          =$rows->transaction_id;
              $joinid                      =$rows->joinpgm_id;
              $pgmname                     =stripslashes($rows->program_url);
              if (empty($transid[0]))
                       {
                     $transid[0]                =$id;
                     $transid[2]                =$joinid;
                     $transid[1]                =$pgmname;
                     }

              $tranStat                =GetTransaction($transid[0],$currValue,$default_currency_caption);   //getting Transaction Statistics from transaction.php
              $tranStat                =explode('~',$tranStat);
              //echo "$transid[2]";
              $details                = GetAffiliateDetails($transid[2]);  //getting Affiliate Staistics   from transaction.php
              $details                =explode('~',$details);
               ?>



            <br/>
            <form name="GetTransaction" method="post" action="index.php?Act=reversesale&amp;pgmid=<?=$pgmid?>">

            <!-- table  1-->
            <table border="0" cellpadding="0" cellspacing="0"  width="90%" align= "center"class="tablebdr1">
                 <tr>
                      <td width="100%" class="tdhead" colspan="2" align="center"><?=$lang_reverse_totalsale?></td>
                 </tr>
                 <tr>

                       <td width="40%" height="20" align="left"  ><?=$lang_report_pgm?>:<?=stripslashes($transid[1])?></td>
                       <td width="60%" height="30" align="right" ><?=$lang_report_transaction?>
                          <select name="transid" onchange="document.GetTransaction.submit()">
                           <?  while($row=mysql_fetch_object($result))
                                           {
                                           $transaction=$row->transaction_id."~".stripslashes($row->program_url)."~".$row->joinpgm_id;
                                           if($transid[0]=="$row->transaction_id")
                                                  $pgmName="selected = 'selected'";
                                           else
                                            $pgmName="";

                                           ?>
                                             <option <?=$pgmName?> value="<?=$transaction?>"> <?=$row->transaction_type?>&nbsp;pgmID=<?=$row->program_id;?> </option>
                                           <?
                                           }
                                           ?>
                         </select>
                       </td>
                 </tr>
                 <tr>
                        <td width="60%" align="center" >

                              <!--  table  2-->
                            <table border="0" cellpadding="0"  width="90%" class="tablebdr" >
                              <tr>
                                <td width="100%" colspan="2" class="tdhead"><?=$lang_reverse_details?></td>
                              </tr>
                               <tr>
                                <td width="50%" height="20" class="grid1"><?=$lang_reverse_name?></td>
                                <td width="50%" height="20" class="grid1"><?=$details[0]?></td>
                              </tr>
                              <tr>
                                <td width="50%" height="20" class="grid1"><?=$lang_reverse_company?></td>
                                <td width="50%" height="20" class="grid1"><?=$details[1]?></td>
                              </tr>
                               <tr>
                                <td width="50%" height="20" class="grid1"><?=$lang_reverse_url?></td>
                                <td width="50%" height="20" class="grid1"><?=$details[2]?></td>
                              </tr>
                            </table>
                              <!-- close table  2-->

                       </td>
                       <td width="40%" align="center">

                                <!--    table  3-->
                             <table border="0" cellpadding="0"   width="90%" class="tablebdr"  >
                                <tr>
                                  <td width="100%" class="tdhead" colspan="2" ><?=$lang_reverse_sale?> </td>
                                </tr>
                               <tr>
                                  <td width="34%" class="grid1" height="20" ><?=$lang_reverse_status?></td>
                                  <td width="40%" align="center"  class="grid1" height="20"><?=$tranStat[3]?></td>
                                </tr>
                                <tr>
                                  <td width="35%"  class="grid1" height="20"><?=$lang_reverse_commission?></td>
                                  <td width="39%" align="center"  height="20" class="grid1" ><?=$tranStat[1]?> <?=$currSymbol?></td>
                                </tr>

                                <tr>
                                  <td width="35%" class="grid1" height="20" >&nbsp;<?=$lang_reverse_date?></td>
                                  <td width="39%" align="center" height="20" class="grid1"><?=$tranStat[2]?></td>
                                </tr>
                               </table>
                                 <!-- close table 3-->

                        </td>
                 </tr>
                 <tr>
                       <td width="100%" height="20" colspan="3"></td>
                 </tr>
                 <tr>
                      <td width="100%" height="20"  colspan="3" align="center">
                             <input type="submit" name="mode" value="Reverse Sale" title="<?=$lang_reversesale?>" style="width: 100" onclick="return confirmDelete()"/>

                      </td>
                 </tr>
                 <tr>
                     <td width="100%" height="20" colspan="3"></td>
                 </tr>
				 <tr>
                     <td width="100%" height="20"   align="center" class="tdhead" colspan="3">

                             </td>
                 </tr>
            </table>
            <!-- close table  1-->

            </form>
           <?php
  }
  else
             {
            ?>

              <!--  table  4-->
             <table width="100%" align="center">
             <tr>
                <td align="center" class="red"><?=$norec?>
                </td>
             <tr>
             </table>
               <!-- close table 4-->

             <?
             }?>


<script language="javascript" type="text/javascript">
   function confirmDelete()
           {
           var del=window.confirm("<?=$lang_reverse_confirm?>") ;
           if (del)
              return true;
           else
              return false;
         }
</script>